//
//  IndexViewController.m
//  薄荷家具
//
//  Created by dllo on 15/12/21.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "IndexViewController.h"
#import "ZHLNewWorking.h"
#import "IndexTitle.h"
#import "ChangeTitleAnimation.h"
#import "Loops.h"
#import "LoopsView.h"
#import "Items.h"
#import "TitleCollectionViewCell.h"

@interface IndexViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,ChangeTitleAnimationDelegate>

@property(nonatomic,retain)NSMutableArray *candidatesArray;
@property(nonatomic,retain)NSMutableArray *channelsArray;
/**
 *  存标题头的名字
 */
@property(nonatomic,retain)NSMutableArray *titleArray;
/**
 *  存轮播图的照片地址
 */
@property(nonatomic,retain)NSMutableArray *loopsArray;
/**
 *  大字典,存导航栏所有的数据
 */
@property(nonatomic,retain)NSMutableArray *bigArray;
/**
 *  一个大的collectionView
 */
@property(nonatomic,retain)UICollectionView *bigCollectionView;
/**
 *  changeTitle
 */
@property(nonatomic,retain)ChangeTitleAnimation *changeTitle;
/**
 *  下拉更新后的接口
 */
@property(nonatomic,retain)NSString *next_url;


/**
 *  设置一个标记,查看网络请求都成功后在走reloadata
 */
@property (nonatomic, assign) NSInteger flag;
@end

@implementation IndexViewController
-(void)dealloc
{
    [_candidatesArray release];
    [_channelsArray release];
    [_loopsArray release];
    [_titleArray release];
    [_bigCollectionView release];
    [_bigArray release];
    [_changeTitle release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.361 green:0.847 blue:0.710 alpha:1];
#pragma mark 初始化
    self.candidatesArray = [NSMutableArray array];
    self.channelsArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
    self.loopsArray = [NSMutableArray array];
    self.bigArray = [NSMutableArray array];

    
    self.navigationItem.title = @"朗朗家居";
#pragma mark 创建大的collectionView
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    NSLog(@"%.2f",self.view.frame.size.height);
    
    self.bigCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,30 * sHeight, kWidth,kHeight - 30 * sHeight - 64 - 40) collectionViewLayout:layout];
    layout.itemSize = CGSizeMake(kWidth, kHeight - 64 - 70);
    layout.minimumInteritemSpacing = 0.0f;
    layout.minimumLineSpacing = 0.0f;
    self.bigCollectionView.delegate = self;
    self.bigCollectionView.dataSource = self;
    self.bigCollectionView.pagingEnabled = YES;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    [self.bigCollectionView registerClass:[TitleCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    
    self.bigCollectionView.tag = 10;
    [self.view addSubview:self.bigCollectionView];
    [_bigCollectionView release];
    [layout release];
    
    
#pragma mark 解析导航栏的数据
    [ZHLNewWorking GetDataWithURL:@"http://api.liwushuo.com/v2/channels/preset?gender=1&generation=4" dic:nil sussess:^(id responseObject) {
        
    
        NSDictionary *dictionary = [responseObject objectForKey:@"data"];
        NSArray *arr = [dictionary objectForKey:@"candidates"];
        for (NSDictionary *dic in arr) {
            
            
            IndexTitle *title = [[IndexTitle alloc] init];
            [title setValuesForKeysWithDictionary:dic];
            [self.candidatesArray addObject:title];
            
            
        }
        
        for (NSDictionary *dic in [dictionary objectForKey:@"channels"]) {
            IndexTitle *title = [[IndexTitle alloc] init];
            [title setValuesForKeysWithDictionary:dic];
            [self.channelsArray addObject:title];
            [self.titleArray addObject:title.name];
#pragma mark 解析 大数组里的东西
            [ZHLNewWorking GetDataWithURL:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?ad=2&gender=1&generation=4&limit=20&set=0",title.id] dic:nil sussess:^(id responseObject) {
                
                NSLog(@"🐶%@",title.id);
                NSDictionary *dic = responseObject[@"data"];
                
                NSMutableArray *arr = [NSMutableArray array];
                
                for (NSDictionary *dictionary in dic[@"items"]) {
                    
                    Items *item = [[Items alloc] init];
                    [item setValuesForKeysWithDictionary:dictionary];
                    [arr addObject:item];
                }
                
                NSString *ID = title.id;
                NSDictionary *smallDic = [NSDictionary dictionaryWithObjectsAndKeys:arr,@"arr",ID,@"ID", nil ];
            
                [self.bigArray addObject:smallDic];
                
                [self.bigCollectionView reloadData];
                // 因为bigCollectionView的数据源是bigArray但是,我们在cellforitem方法里要用到jiexilunbo方法里的网络请求去解析loopsArray,在那里reloadata
                

            } filed:^(NSError *error) {
                
            }];
            
            
            
            
        }
//        
        ChangeTitleAnimation *title = [[ChangeTitleAnimation alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,30 * sHeight) titleArray:self.titleArray titleColor:[UIColor blackColor] titleSelectedColor:[UIColor colorWithRed:0.361 green:0.847 blue:0.710 alpha:1] lineColor:[UIColor colorWithRed:0.361 green:0.847 blue:0.710 alpha:1] sizeOfFont:15];
        title.delegate = self;
        self.changeTitle = title;
        [self.view addSubview:title];
    
//        [self.bigCollectionView reloadData];
    } filed:^(NSError *error) {
        NSLog(@"%@",error);
    }];

#pragma mark 解析轮播图

        [ZHLNewWorking GetDataWithURL:@"http://api.liwushuo.com/v2/banners?channel=IOS" dic:nil sussess:^(id responseObject) {
            
            
            NSArray *array = [[responseObject objectForKey:@"data"] objectForKey:@"banners"];
            
            for (NSDictionary *dic in array) {
                Loops *loop = [[Loops alloc]init];
                [loop setValuesForKeysWithDictionary:dic];
                [self.loopsArray addObject:loop.image_url];
            }
            
            
            [self.bigCollectionView reloadData];
            
            
            //        LoopsView *loops = [[LoopsView alloc]initWithFrame:CGRectMake(0, 30 * sHeight,kWidth , 150 *sHeight) andArray:self.loopsArray andinterval:2];
            //        NSLog(@"%@",self.loopsArray);
            //        [self.view addSubview:loops];
            //        [loops release];
            
            
        } filed:^(NSError *error) {
            
            NSLog(@"轮播图%@",error);
        }];
        //

    }
  

#pragma mark 改变上面的导航按钮
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger itemCount = scrollView.contentOffset.x / kWidth;
    if (scrollView.tag == 10) {
        [self.changeTitle changeTitleByValue:itemCount + 1];
    }
}

-(void)getPresentTitleNumber:(NSInteger)titleNumber
{
   
    
    self.bigCollectionView.contentOffset = CGPointMake(self.view.frame.size.width * (titleNumber - 1), 0);
    
}
#pragma mark 返回item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.bigArray.count != 0 && self.loopsArray.count != 0) {
    return self.bigArray.count;
    }
    else return 0;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"haha");
    
    TitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor grayColor];
    
    IndexTitle *title = self.channelsArray[indexPath.row];
 
    NSString *twoId = [title.id description];
    
    for (NSDictionary *dic in self.bigArray) {
        
        NSNumber *ID = dic[@"ID"];
        
        NSString *oneId = [ID description];
        
        if ([oneId isEqualToString:twoId]) {
            
            NSMutableArray *arr = dic[@"arr"];
            cell.tableArray = arr;
            cell.currentPage = 20;
           
            if ([twoId isEqualToString:@"108"]) {
                
                cell.loopsArray = self.loopsArray;
            }
            cell.flag = oneId;
            NSLog(@"🐯%@",cell.flag);
            [cell.tableView reloadData];
        }
        
      }

    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
